﻿using System;
using System.Windows.Forms;
using SharePointUtilitiesForDevelopers.Action;
using SharePointUtilitiesForDevelopers.Actions.Forms;

namespace SharePointUtilitiesForDevelopers.Actions
{
    /// <summary>
    /// Generates a url for opening a list view.
    /// </summary>
    public class GenerateListViewUrlAction: IAction
    {
        /// <summary>
        /// Gets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return Properties.Resources.GenerateListViewUrlActionTitle; }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return Properties.Resources.GenerateListViewUrlActionDescription; }
        }

        /// <summary>
        /// Gets the target.
        /// </summary>
        /// <value>The target.</value>
        public ActionTarget Target
        {
            get { return ActionTarget.List | ActionTarget.ListView; }
        }

        /// <summary>
        /// Executes the action.
        /// </summary>
        /// <param name="actionContext">The context.</param>
        public void Execute(ActionContext actionContext)
        {
            string url = null;

            if(actionContext.Entity is SharePointList)
            {
                var list = (SharePointList)actionContext.Entity;
                url = GenerateListViewUrl(actionContext.Web.Url, list);
            }

            if (actionContext.Entity is SharePointListView)
            {
                var view = (SharePointListView)actionContext.Entity;
                url = GenerateListViewUrl(actionContext.Web.Url, view);
            }

            if(url != null)
            {
                var form = ServiceLocator.Resolve<UrlForm>();
                form.Url = url;
                form.UrlDescription = Description;
                form.ShowDialog(actionContext.Owner as IWin32Window);
            }
        }

        /// <summary>
        /// Generates the list view URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        private static string GenerateListViewUrl(string siteUrl, SharePointList list)
        {
            return GetViewUrl(siteUrl, list.DefaultViewUrl);
        }

        /// <summary>
        /// Generates the list view URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="view">The view.</param>
        /// <returns></returns>
        private static string GenerateListViewUrl(string siteUrl, SharePointListView view)
        {
            return GetViewUrl(siteUrl, view.Url);
        }

        /// <summary>
        /// Gets the view url.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="viewUrl">The view URL.</param>
        /// <returns>The url to the view.</returns>
        private static string GetViewUrl(string siteUrl, string viewUrl)
        {
            // Return the service url (site + service url)
            var siteUri = new Uri(siteUrl);
            var viewUri = new Uri(siteUri, viewUrl);
            return viewUri.ToString();
        }
    }
}